library MOSCommand initializer init requires LegacySystem

	globals
		private boolean MOSEnabled = false
		private boolean array MOSSet
	endglobals
	
	private function MOSCommandA takes nothing returns nothing
		local string s = GetEventPlayerChatString()
		local string mosNumber 
		local integer i = 0
		local integer m = 0
		local integer pid = GetPlayerId(GetTriggerPlayer()) + 1
		local player p = GetTriggerPlayer()
		if s == "-mos" then
			loop
				exitwhen i > 7 
				call DisplayTimedTextToPlayer(p,0.0,0.0,20.0, "MOS " + I2S(i) + ": " + udg_MOSText[i])
				set i = i + 1
			endloop
		else
			if MOSSet[pid] then
				call DisplayTimedTextToPlayer(p,0.0,0.0,20.0, "You have already selected a MOS. You must complete another tour before reassignment." )
			elseif MOSEnabled then
				set mosNumber = SubString(s, 5 , 6)
				set m = S2I(mosNumber)
				if m > 0 and m <= 7 then
					set MOSSet[pid] = true
					call DisplayTimedTextToPlayer(p,0.0,0.0,5.0, "MOS set to " + udg_MOSText[m])
					call SetI("player mos", pid, m)
				else
					call DisplayTimedTextToPlayer(p,0.0,0.0,5.0, "Invalid MOS Selection; must be a number ranging from 1 to 7." )
				endif
			else
				// display disabled message
				call DisplayTimedTextToPlayer(p,0.0,0.0,20.0, "MOS cannot be selected at this time. Selection is only available upon successful tour completion." )
			endif
		endif
	endfunction

	function SetMOSEnabled takes boolean b returns nothing
		set MOSEnabled = b
	endfunction
	
	private function init takes nothing returns nothing
		local trigger t
		local integer i = 0
		set t = CreateTrigger()
		
		loop
			exitwhen i > 11
			call TriggerRegisterPlayerChatEvent(t, Player(i), "-mos ", false)
			call TriggerRegisterPlayerChatEvent(t, Player(i), "-mos", true)
			set i = i + 1
		endloop

		call TriggerAddAction(t,function MOSCommandA)
	endfunction
endlibrary